package com.erp.module.crm.xXCrm.baoMingXi

import android.annotation.SuppressLint
import android.os.AsyncTask
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.erp.activity.BaseActivity
import com.erp.myAdapter.BaomingxiAdapter
import com.erp.bean.ProdyctionClientBean
import com.erp.mclass.UnCeHandler
import com.erp.R
import com.erp.module.crm.xXCrm.FragmentClientfive
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_baomingxi.*
import kotlinx.android.synthetic.main.header_knoya.view.*
import java.util.*

/**
 * Created by Administrator on 2016/12/28/028.
 * 作者：刘洋
 */
class BaomingxiKActivity : BaseActivity(), View.OnClickListener {


    lateinit var listbaomingxi: List<ProdyctionClientBean.baomingxi>
    lateinit var mlistbaomingxi: MutableList<ProdyctionClientBean.baomingxi>
    lateinit var baomingxiAdapter: BaomingxiAdapter
    private var onum: String = ""
    lateinit var listzhuangtai: MutableList<String>
    lateinit var listguihao: MutableList<String>
    private var sp1 = 0
    private var sp2 = 0
    private val a = "BaomingxiActivity"
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_baomingxi
    }

    override fun initCreate() {
        initview()
        zhaungtaispinner()
        guihaospinner()
    }
    private fun initview() {
        UnCeHandler.buzhou = "initview"+onum
        baomingxiHerder.iv_header_left.visibility = View.VISIBLE
        baomingxiHerder.iv_header_left.setOnClickListener(this)
        onum = intent.getStringExtra("shengchan")
        baomingxiHerder.tv_header_title.text = onum
        listguihao = ArrayList()
        listzhuangtai = ArrayList()
        listbaomingxi = ArrayList()
        mlistbaomingxi = ArrayList()
        listbaomingxi = FragmentClientfive.getbaomingxi()
        baomingxiAdapter = BaomingxiAdapter(mlistbaomingxi, this)
        lvBaomingxi.adapter = baomingxiAdapter
        lvBaomingxi.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            UnCeHandler.buzhou = "OnItemClickListener"+onum
            baomingxiAdapter.itemposition(position) }
    }
    private fun guihaospinner() {
        UnCeHandler.buzhou = "guihaospinner"+onum
        val ayncTask = @SuppressLint("StaticFieldLeak")
        object : AsyncTask<Void, Void, List<String>>() {
            override fun doInBackground(vararg params: Void?): List<String> {
                UnCeHandler.buzhou = "guihaospinner1"+onum
                listguihao.add("全部")
                for (i in listbaomingxi.indices) {
                    val ist = listguihao.indices.none { listbaomingxi[i].guihao == listguihao[it] }
                    if (ist) {
                        listguihao.add(listbaomingxi[i].guihao)
                    }
                }
                return listguihao
            }

            override fun onPostExecute(result: List<String>?) {
                UnCeHandler.buzhou = "guihaospinner3"+onum
                val shengfenadapter = ArrayAdapter<String>(this@BaomingxiKActivity, R.layout.lv_sp_tvitem, listguihao)
                spGuihao.adapter = shengfenadapter
            }
        }
        ayncTask.execute()
        spGuihao.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                UnCeHandler.buzhou = "guihaospinner2"+onum
                mlistbaomingxi.clear()
                sp1 = position
                for (i in listbaomingxi.indices) {
                    if (sp2 == 0) {
                        if (listguihao[sp1] == listbaomingxi[i].guihao) {
                            mlistbaomingxi.add(listbaomingxi[i])
                        } else if (listguihao[sp1] == "全部") {
                            mlistbaomingxi.add(listbaomingxi[i])
                        }
                    } else {
                        if (listguihao[sp1] == listbaomingxi[i].guihao && listzhuangtai[sp2] == listbaomingxi[i].zhuangtai) {
                            mlistbaomingxi.add(listbaomingxi[i])
                        } else if (listguihao[sp1] == "全部" && listzhuangtai[sp2] == listbaomingxi[i].zhuangtai) {
                            mlistbaomingxi.add(listbaomingxi[i])
                        }
                    }
                }
                baomingxiAdapter.notifyall(mlistbaomingxi)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
    }

    private fun zhaungtaispinner() {
        UnCeHandler.buzhou = "zhaungtaispinnexr"+onum
        val ayncTask = @SuppressLint("StaticFieldLeak")
        object : AsyncTask<Void, Void, List<String>>() {
            override fun doInBackground(vararg params: Void): List<String> {
                UnCeHandler.buzhou = "doInBackground"+onum
                listzhuangtai.add("全部")
                for (i in listbaomingxi.indices) {
                    val ist = listzhuangtai.indices.none { listbaomingxi[i].zhuangtai == listzhuangtai[it] }
                    if (ist) {
                        listzhuangtai.add(listbaomingxi[i].zhuangtai)
                    }
                }
                return listzhuangtai
            }

            override fun onPostExecute(list: List<String>) {
                UnCeHandler.buzhou = "onPostExecutex"+onum
                val shengfenadapter = ArrayAdapter<String>(this@BaomingxiKActivity, R.layout.lv_sp_tvitem, listzhuangtai)
                spZhuangtai.adapter = shengfenadapter
            }
        }
        ayncTask.execute()
        spZhuangtai.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                UnCeHandler.buzhou = "onItemSelected"+onum
                mlistbaomingxi.clear()
                sp2 = position
                for (i in listbaomingxi.indices) {
                    if (sp1 == 0) {
                        if (listzhuangtai[sp2] == listbaomingxi[i].zhuangtai) {
                            mlistbaomingxi.add(listbaomingxi[i])
                        } else if (listzhuangtai[sp2] == "全部") {
                            mlistbaomingxi.add(listbaomingxi[i])
                        }
                    } else {
                        if (listguihao[sp1] == listbaomingxi[i].guihao && listzhuangtai[sp2] == listbaomingxi[i].zhuangtai) {
                            mlistbaomingxi.add(listbaomingxi[i])
                        } else if (listguihao[sp1] == listbaomingxi[i].guihao && listzhuangtai[sp2] == "全部") {
                            mlistbaomingxi.add(listbaomingxi[i])
                        }
                    }
                }
                baomingxiAdapter.notifyall(mlistbaomingxi)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) = Unit
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            baomingxiHerder.iv_header_left -> {
                finish()
            }
        }
    }
}
